﻿
using MauiApp1.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace MauiApp1.ViewModels
{
    public class MainPageViewModel: BindableObject
    {
        private ObservableCollection<Cate> _GoodsList;
        public ObservableCollection<Cate> GoodsList
        {
            get { return _GoodsList; }
            set { _GoodsList = value; OnPropertyChanged(nameof(GoodsList)); }
        }


        private ObservableCollection<Video> _VideoList;
        public ObservableCollection<Video> VideoList
        {
            get { return _VideoList; }
            set { _VideoList = value; OnPropertyChanged(nameof(VideoList)); }
        }


        private int _CateId;
        public int CateId
        {
            get { return this._CateId; }
            set { _CateId = value; OnPropertyChanged(nameof(CateId)); }
        }

        private int _PageIndex;
        public int PageIndex
        {
            get { return _PageIndex; }
            set { _PageIndex=value; OnPropertyChanged(nameof(PageIndex)); }
        }

        private string _KeyWord;
        public string KeyWord
        {
            get { return _KeyWord; }
            set { _KeyWord = value; OnPropertyChanged(nameof(KeyWord)); }
        }



        private  bool _IsLoading;
        public  bool IsLoading
        {
            get { return _IsLoading; }
            set { _IsLoading = value; OnPropertyChanged(nameof(IsLoading)); }
        }


        private bool _HaveData;
        public bool HaveData
        {
            get { return _HaveData; }
            set { _HaveData = value; OnPropertyChanged(nameof(HaveData)); }
        }

        private ICommand _LoadCommand;
        public ICommand LoadCommand=> _LoadCommand??=new Command(LoadVideo);


        private ICommand _ClickCateCommand;
        public ICommand ClickCateCommand => _ClickCateCommand ??= new Command(GetCateVideo);

        private ICommand _InfoCommand;
        public ICommand InfoCommand => _InfoCommand ??= new Command(GoToVideoInfo);

        public MainPageViewModel()
        {
            HaveData = true;
            IsLoading = false;
            GoodsList = new ObservableCollection<Cate>();
            VideoList=new ObservableCollection<Video>();
           
            InitData();
        }


        public async void InitData()
        {
            HaveData = true;
            IsLoading = false;
            CateId = 0;
            PageIndex = 1;
            KeyWord = "";
            await GetCate();
            await GetVideoList();
        }

        public async Task GetCate()
        {
            var res=await APIService.GetCate();
            if(res!=null && res.typeList!=null)
            {
                CateId = res.typeList.FirstOrDefault().type_id;
                GoodsList.Clear();
                foreach(var item in res.typeList) {  GoodsList.Add(item); }
            }
        }

        public async Task GetVideoList()
        {
            var res = await APIService.GetVideo(CateId,PageIndex,KeyWord);
            if(res!=null && res.list != null)
            {
                VideoList.Clear();
                foreach(var item in res.list) {  VideoList.Add(item); }
            }
        }

        

        public  async void LoadVideo(object obj)
        {
            if (!IsLoading && HaveData)
            {
                IsLoading=true;
                PageIndex++;
                var res = await APIService.GetVideo(CateId, PageIndex, KeyWord);
                if (res != null && res.list != null && res.list.Any())
                {
                    foreach (var item in res.list) { VideoList.Add(item); }
                }
                else
                {
                    HaveData = false;
                }
                
            }
            IsLoading = false;
        }

        public async void GetCateVideo(object obj)
        {
            var cate = obj as Cate;
            if (cate != null)
            {
                CateId = cate.type_id;
                PageIndex = 1;
                IsLoading = false;
                HaveData=true;
                KeyWord = "";
                await GetVideoList();

            }
        }

        public async void GoToVideoInfo(object obj)
        {
            var video=obj as Video;
            if(video != null)
            {
                var parameter = new Dictionary<string, object>()
                {
                    {"VideoId",video.vod_id }
                };
                await Shell.Current.GoToAsync("VideoInfoPage",parameter);
            }
        }

        private ICommand _SearchCommand;
        public ICommand SearchCommand => _SearchCommand ??= new Command(Search);

        public async void Search(object obj)
        {
            CateId = 0;
            PageIndex = 1;
            IsLoading = false;
            HaveData = true;
            await GetVideoList();
        }
    }
}
